package com.xyq.friend.aspect;

import com.xyq.common.core.constants.Constants;
import com.xyq.common.core.enums.ResultCode;
import com.xyq.common.core.exception.ServiceException;
import com.xyq.common.core.utils.ThreadLocalUtil;
import com.xyq.friend.domain.user.vo.UserVO;
import com.xyq.friend.manager.UserCacheManager;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Aspect
@Component
public class StatusAspect {

    @Autowired
    private UserCacheManager userCacheManager ;

    @Before("@annotation(com.xyq.friend.aspect.CheckStatus)")
    public  void check() {
        // 判断当前用户的状态, 看当前用户是否已经被拉黑
        // 获取用户id
        Long userId = ThreadLocalUtil.get(Constants.USER_ID, Long.class);
        //从 缓存里获取 当前用户的数据
        UserVO userVO = userCacheManager.getUserById(userId);
        if(userVO.getStatus().equals(Constants.FALSE)){
            throw new ServiceException(ResultCode.FAILED_USER_BANNED);
        }
    }
}
